﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;

namespace School.Pages.Uniforms
{
    public partial class ClothesPricing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1401) == true)
                {
                    DropDownList ddl = ((DropDownList)(CampusPackage1.FindControl("ddlGrade")));
                    ddl.AutoPostBack = true;
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
                    lblResult.Text = "";
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList Gradeddl = (DropDownList)sender;
            if (Gradeddl.SelectedIndex != 0)
            {
                if (CampusPackage1.CampusId != "0" && CampusPackage1.CertificateId != "0" && CampusPackage1.GradeId != "0" && CampusPackage1.StageId != "0")
                {
                    ddlClothes.DataSource = School.Service.Clothes.SRV_CLothe.AllClothes().Where(s => s.Campus_Id == long.Parse(CampusPackage1.CampusId))
                        .Where(s => s.Cer_Id == long.Parse(CampusPackage1.CertificateId)).Where(s => s.Stage_Id == long.Parse(CampusPackage1.StageId))
                        .Where(s => s.Grad_Id == long.Parse(CampusPackage1.GradeId));
                    ddlClothes.DataBind();
                    ddlClothes.TextChanged += new EventHandler(ddlClothes_TextChanged);
                }
            }
        }

      protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            List<SRV_Clothe_Price> lstPrices = new List<SRV_Clothe_Price>();
            TextBox txbprice;
            if (GridView1.Rows.Count > 0)
                foreach (GridViewRow item in GridView1.Rows)
                {
                    if (item.RowType == DataControlRowType.DataRow)
                    {
                        txbprice = (TextBox)item.Cells[3].FindControl("txtPrice");
                        lstPrices.Add(new SRV_Clothe_Price()
                        {
                            //Price_Id = long.Parse(GridView1.DataKeys[item.RowIndex].Values["Id"].ToString()),
                            Size_Id = int.Parse(GridView1.DataKeys[item.RowIndex].Values["Size_Id"].ToString()),
                            Price = double.Parse(txbprice.Text),
                            DateTime = DateTime.Now,

                        });
                    }
                }
            if (School.Service.Uniform.SRV_Clothe_Price.InsertClothe_Price(lstPrices))
            {
                lblResult.ForeColor = Color.Green;
                lblResult.Text = "تمت عمليه الاضافه بنجاح";
                ClearGrid();

            }
        }
        public void ClearGrid()
        {
            foreach (GridViewRow item in GridView1.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    TextBox txbprice = (TextBox)item.Cells[3].FindControl("txtPrice");
                    txbprice.Text = "";
                }
            }

        }

        protected void ddlClothes_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlClothes.SelectedValue))
            {
                GridView1.DataSource = School.Service.Clothes.SRV_Clothe_Sizes_Size.AllClothesSizesByClotheId(int.Parse(ddlClothes.SelectedValue));
                GridView1.DataBind();
            }
        }

        protected void btnSaveStation_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlClothes.SelectedValue))
            {
                GridView1.DataSource = School.Service.Clothes.SRV_Clothe_Sizes_Size.AllClothesSizesByClotheId(int.Parse(ddlClothes.SelectedValue));
                GridView1.DataBind();
            }
        }

        protected void ddlClothes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlClothes.SelectedValue !="0")
            {
                GridView1.DataSource = School.Service.Clothes.SRV_Clothe_Sizes_Size.AllClothesSizesByClotheId(int.Parse(ddlClothes.SelectedValue));
                GridView1.DataBind();
            }
        }
    }
}